<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增优惠券')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-coupons-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">满XXX元  限制：</label>
                <div class="col-sm-8">
                    <input name="mlimit" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">实减金额：</label>
                <div class="col-sm-8">
                    <input name="jnumber" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图片：</label>
                <div class="col-sm-8">
                    <input id="img" type="hidden" name="img">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="file" name="file" type="file" accept=".jpg, .png, .jpeg">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">活动开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input id="startTime" name="startTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">活动结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input id="endTime" name="endTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "dl/coupons"
        $("#form-coupons-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-coupons-add').serialize());
            }
        }

        /*$("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            minView: "month",
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            minView: "month",
            autoclose: true
        });*/

        $("#file").fileinput({
            uploadUrl: prefix + "/upload", //上传的地址
            showPreview: true, //是否显示预览
            maxFileCount: 1, //表示允许同时上传的最大文件个数
            enctype: 'multipart/form-data',
            maxFileSize: 0,
            allowedPreviewTypes: ['image'],
            allowedFileTypes: ['image'],
            allowedFileExtensions : ['jpg', 'png', 'jpeg'],
            previewFileIcon: "<i class='glyphicon glyphicon-king'></i>"
        }).on('fileuploaded', function (event, data, previewId, index) {
            //同步上传成功回调，如果是异步上传成功会回调fileuploaded，用错的话，回调是不会执行的
            //通过 data.response.Json对象属性 获得返回数据
            if (data.response.code == 0) {
                $("#img").val(data.response.data)
                console.log(data.response.data)
            } else {
                $.modal.alertWarning(data.response.msg);
            }
        });

        layui.use('laydate', function() {
            var laydate = layui.laydate;

            var startDate = laydate.render({
                elem: '#startTime',
                max: $('#endTime').val(),
                theme: 'molv',
                trigger: 'click',
                done: function(value, date) {
                    // 结束时间大于开始时间
                    if (value !== '') {
                        endDate.config.min.year = date.year;
                        endDate.config.min.month = date.month - 1;
                        endDate.config.min.date = date.date;
                    } else {
                        endDate.config.min.year = '';
                        endDate.config.min.month = '';
                        endDate.config.min.date = '';
                    }
                }
            });

            var endDate = laydate.render({
                elem: '#endTime',
                min: $('#startTime').val(),
                theme: 'molv',
                trigger: 'click',
                done: function(value, date) {
                    // 开始时间小于结束时间
                    if (value !== '') {
                        startDate.config.max.year = date.year;
                        startDate.config.max.month = date.month - 1;
                        startDate.config.max.date = date.date;
                    } else {
                        startDate.config.max.year = '';
                        startDate.config.max.month = '';
                        startDate.config.max.date = '';
                    }
                }
            });
        });
    </script>
</body>
</html>